package com.wxd.controller;

import com.wxd.model.User;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author wxd
 * @version V1.0
 * @description SwaggerController
 * @date 2022/4/13 10:38
 **/
@RestController
/**
 * @Api：用在Controller上，表示对类的说明
 * tags="说明该类的作用，可以在UI界面上看到的注解"
 * value="该参数没什么意义，在UI界面上也看到，所以不需要配置"
 */
@Api(tags = "api tag", value = "api value")
@Slf4j
public class SwaggerController {
    /**
     * @ApiOperation：用在请求的方法上，说明方法的用途、作用 value="说明方法的用途、作用"
     * notes="方法的备注说明"
     */
    @ApiOperation(value = "获取用户", notes = "方法的备注说明")
    @ApiResponses({@ApiResponse(code = 200, message = "获取用户列表成功")})
    @GetMapping("user")
    public ResponseEntity<User> getUser() {
        User user = new User();
        user.setName("知识追寻者");
        user.setHobby("爱睡觉");
        user.setEmail("不告诉你");
        return ResponseEntity.ok(user);
    }

    @ApiOperation(value = "跟据用户编号获取用户")
    /**
     * @ApiResponses：用在请求的方法上，表示一组响应
     * @ApiResponse：用在@ApiResponses中，一般用于表达一个错误的响应信息
     * code：http错误码，例如400
     * message：信息，例如"请求参数没填好"
     * response：抛出异常的类
     */
    @ApiResponses({@ApiResponse(code = 200, message = "获取用户列表成功"), @ApiResponse(code = 204, message = "没有内容")})
    /**
     * @ApiImplicitParams：用在请求的方法上，表示一组参数说明
     * @ApiImplicitParam：用在@ApiImplicitParams注解中，指定一个请求参数的各个方面
     * name：参数名
     * value：参数的汉字说明、解释
     * required：参数是否必须传
     * paramType：参数放在哪个地方
     *  header --> 请求参数的获取：@RequestHeader
     *  query --> 请求参数的获取：@RequestParam
     *  path（用于restful接口）--> 请求参数的获取：@PathVariable
     *  body（不常用）
     *  form（不常用）
     */
    @ApiImplicitParam(name = "id", value = "用户编号", dataType = "Long", required = true, paramType = "path")
    @GetMapping("user/{id}")
    public ResponseEntity<User> getUserById(@PathVariable Long id) {
        log.info("用户id:{}", id);
        User user = new User();
        user.setName("知识追寻者");
        user.setHobby("爱睡觉");
        user.setEmail("不告诉你");
        return ResponseEntity.ok(user);
    }

    @PostMapping("user")
    @ApiResponse(code = 201, message = "添加用户列表成功")
    @ApiOperation(value = "添加用户")
    public ResponseEntity<String> addUser(@RequestBody User user) {

        log.info("save the user:" + user);
        return ResponseEntity.ok("success save the user");
    }

    @ApiResponse(code = 200, message = "修改用户成功")
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PutMapping("user/{id}")
    // 参数信息 多个参数使用注释中得内容, @RequestBody 修斯参数没必要写
    /*@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户编号", dataType = "Long",required = true,paramType = "path")
    ,@ApiImplicitParam(name = "user", value = "用户", dataType = "User",required = true, paramType = "json")})*/
    @ApiImplicitParam(name = "id", value = "用户编号", dataType = "Long", required = true, paramType = "path")
    public ResponseEntity<String> updateUser(@PathVariable Long id, @RequestBody User user) {

        log.info("update the user:" + user);
        return ResponseEntity.ok("success update the user,the number is :" + id);
    }


    @ApiResponses({@ApiResponse(code = 200, message = "删除用户成功")
            , @ApiResponse(code = 401, message = "没有权限")
    })
    @ApiOperation(value = "删除用户")
    @DeleteMapping("user/{id}")
    @ApiImplicitParam(name = "id", value = "用户编号", dataType = "Long", required = true, paramType = "path")
    public ResponseEntity<String> updateUser(@PathVariable Long id) {
        log.info("delete the user :" + id);
        return ResponseEntity.ok("delete the user :" + id);
    }
}
